<!-- 用户管理 -->
<template>
  <div class="app-container">
    <div class="search-bar">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item label="关键字" prop="keywords">
          <el-input
            v-model="queryParams.keywords"
            placeholder="用户名/昵称/手机号"
            clearable
            style="width: 200px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>

        <el-form-item label="状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="全部" clearable class="!w-[100px]">
            <el-option label="正常" :value="1" />
            <el-option label="禁用" :value="0" />
          </el-select>
        </el-form-item>

        <el-form-item label="创建时间">
          <el-date-picker
            v-model="queryParams.createTime"
            :editable="false"
            class="!w-[240px]"
            type="daterange"
            range-separator="~"
            start-placeholder="开始时间"
            end-placeholder="截止时间"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="search" @click="handleQuery">搜索</el-button>
          <el-button icon="refresh" @click="handleResetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="flex flex-col h-full" body-style="height: calc(100% - 0px);">
      <BasePageTable
        :tableColumnsData="columns"
        :tableData="pageData"
        :total="total"
        :loading="loading"
        :pageNum="queryParams.pageNum"
        :pageSize="queryParams.pageSize"
        class="h-full"
        @handleQuery="handlePageQuery"
      >
        <template #index="scope">
          {{ scope.index + 1 }}
        </template>
        <template #roleNames="scope">
          <el-tag>
            {{ scope.row.roleNames }}
          </el-tag>
        </template>
        <template #status="scope">
          <el-switch
            v-model="scope.row.status"
            inline-prompt
            active-text="正常"
            inactive-text="禁用"
            :active-value="1"
            :inactive-value="0"
            :before-change="() => handleStatusChange(scope.row.id, scope.row.status)"
          />
        </template>
        <template #operation="scope">
          <el-button link type="primary" @click="handleGreet(scope.row)">设置佣金</el-button>
        </template>
      </BasePageTable>
      <el-dialog
        v-model="dialog.visible"
        title="设置佣金"
        width="600px"
        @close="handleCloseDialog"
        :close-on-click-modal="false"
      >
        <el-form
          ref="formRef"
          :model="formData"
          :rules="formRules"
          label-width="150px"
          class="form"
        >
          <el-form-item label="佣金	" prop="consumerAgentCommission">
            <el-input-number
              v-model="formData.consumerAgentCommission"
              placeholder="请输入佣金金额"
              style="width: 260px"
              :max="800"
              :min="0"
              :step="10"
              step-strictly
              controls-position="right"
            />
            <el-button>元/人</el-button>
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button type="primary" @click="handleSubmit">确 定</el-button>
            <el-button @click="handleCloseDialog">取 消</el-button>
          </div>
        </template>
      </el-dialog>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { UserPageQuery, UserPageVO } from "@/api/system/user";
import BasePageTable from "@/components/BasePageTable/index.vue";
import { useNimStore } from "@/store/modules/nim.js";
import MerchantUserAPI from "@/api/user/merchantuser";
const { sendMessage } = useNimStore();
// import UserImport from "./components/UserImport.vue";

defineOptions({
  name: "User",
  inheritAttrs: false,
});

const columns = ref<any[]>([
  {
    label: "序号",
    prop: "index",
    customSlot: "index",
    width: 80,
  },
  {
    label: "姓名",
    prop: "nickname",
    align: "center",
    showOverflowTooltip: true,
  },
  {
    label: "手机号码",
    prop: "mobile",
    align: "center",
    showOverflowTooltip: true,
  },
  {
    label: "佣金(元)",
    prop: "merchantEmployeeVO.consumerAgentCommission",
    align: "center",
    width: 150,
    showOverflowTooltip: true,
  },
  {
    label: "对接人",
    prop: "merchantEmployeeVO.recommender",
    align: "center",
    showOverflowTooltip: true,
  },
  {
    label: "状态",
    prop: "status",
    align: "center",
    showOverflowTooltip: true,
    customSlot: "status", // 自定义插槽名称
  },
  {
    label: "创建时间",
    prop: "createTime",
    align: "center",
    showOverflowTooltip: true,
  },
  {
    label: "操作",
    prop: "operation",
    width: 200,
    align: "center",
    fixed: "right",
    showOverflowTooltip: true,
    customSlot: "operation", // 自定义插槽名称
  },
]);
const queryFormRef = ref();
const formRef = ref();
const formRules = reactive({
  consumerAgentCommission: [{ required: true, message: "请输入佣金金额", trigger: "blur" }],
});

const queryParams = reactive<UserPageQuery>({
  pageNum: 1,
  pageSize: 10,
});

const pageData = ref<UserPageVO[]>();
const total = ref(0);
const loading = ref(false);
const handleStatusChange = (value: number, status: number) => {
  try {
    MerchantUserAPI.updateStatus(value, status === 1 ? 0 : 1).then(() => {
      ElMessage.success("状态更新成功");
      handleResetQuery();
    });
    return true;
  } catch (error) {
    console.log(error);
    ElMessage.error("状态更新失败");
    return false;
  }
};
const dialog = reactive({
  visible: false,
  title: "新增用户",
});

const formData = ref({
  employeeId: undefined,
  consumerAgentCommission: undefined,
});

const handlePageQuery = (val: any) => {
  queryParams.pageNum = val.pageNum;
  queryParams.pageSize = val.pageSize;
  handleQuery();
};
// 查询
function handleQuery() {
  loading.value = true;
  MerchantUserAPI.getAgentList(queryParams)
    .then((data: any) => {
      pageData.value = data.list;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

// 关闭弹窗
function handleCloseDialog() {
  dialog.visible = false;
  formData.value.consumerAgentCommission = undefined;
  formRef.value.resetFields();
}

async function handleSubmit() {
  const validate = await formRef.value.validate();
  if (!validate) return;
  try {
    MerchantUserAPI.updataAgentCommission(formData.value).then((res) => {
      ElMessage.success("设置成功");
      dialog.visible = false;
      handleResetQuery();
    });
  } catch (e) {
    ElMessage.error("状态更新失败");
  }
}

// 重置查询
function handleResetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  queryParams.createTime = undefined;
  handleQuery();
}

async function handleGreet(val: any) {
  dialog.visible = true;
  formData.value.employeeId = val.merchantEmployeeVO.id;
}

onMounted(() => {
  handleQuery();
});
</script>
